﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;

namespace KhoBanHang
{
    public partial class DMHang : Form
    {
        List<LoaiHang> danhSachLoaiHang;
        List<HangHoa> danhSachHangHoa;

        public DMHang()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            txtMaHang.Text = "";

            txtTenHang.Text = "";
            txtTenHang.Focus();

            txtDonvitinh.Text = "";
            txtGiaBan.Text = "";

            txtGiavon.Text = "";

            cmbLoaiHang.SelectedItem = null;
            txtMoTa.Text = "";
            txtSoLuongTon.Text = "";
            checkboxTheoDoi.Checked = false;
        }

        private void DMHang_Load(object sender, EventArgs e)
        {
            LoadDanhSachHangHoa();

            LoadDanhSachLoaiHang();

            if (@Session.quyen ==  "2") 
            {
                btnThem.Enabled = false;
                btnLuu.Enabled = false;
            }
        }

        private void LoadDanhSachLoaiHang()
        {
            danhSachLoaiHang = Business.LoaiHang.LayDanhSach();

            cmbLoaiHang.DataSource = danhSachLoaiHang;
            cmbLoaiHang.DisplayMember = "TenLoai";

            cmbLoaiHang.ValueMember = "MaLoai";
            cmbLoaiHang.SelectedItem = null;
        }

        private void LoadDanhSachHangHoa()
        {
            danhSachHangHoa = Business.HangHoa.LayDanhSach();
            gridviewMDHang.DataSource = danhSachHangHoa;

            gridviewMDHang.Columns["MaLoai"].Visible = false;

            gridviewMDHang.Columns["MaHangHienThi"].HeaderText = "Mã hàng";
            gridviewMDHang.Columns["TenHang"].HeaderText = "Tên hàng";
            gridviewMDHang.Columns["TenLoai"].HeaderText = "Loại hàng";

            gridviewMDHang.Columns["SoLuongTon"].HeaderText = "Số lượng tồn";
            gridviewMDHang.Columns["GiaVon"].HeaderText = "Giá vốn";
            gridviewMDHang.Columns["GiaBan"].HeaderText = "Giá bán";
            gridviewMDHang.Columns["MoTa"].HeaderText = "Mô tả";

            gridviewMDHang.Columns["TheoDoi"].HeaderText = "Theo dõi";

            gridviewMDHang.Columns["DonViTinh"].HeaderText = "Đơn vị tính";


        }

        private void gridviewMDHang_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (danhSachHangHoa != null)
            {
                HangHoa hang = danhSachHangHoa[e.RowIndex];

                txtMaHang.Text = hang.MaHangHienThi;
                txtTenHang.Text = hang.TenHang;


                cmbLoaiHang.SelectedValue = hang.MaLoai;
                               
                txtDonvitinh.Text = hang.DonViTinh.ToString();
                txtGiaBan.Text = hang.GiaBan.ToString();
                txtGiavon.Text = hang.GiaVon.ToString();
                
                txtMoTa.Text = hang.MoTa;

                if (hang.TheoDoi == true)

                    checkboxTheoDoi.Checked = true;
                else
                    checkboxTheoDoi.Checked = true;
                txtSoLuongTon.Text = hang.SoLuongTon.ToString();
                
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (KiemTra()==false)
                return;

            bool theodoi;
            if (checkboxTheoDoi.Checked == true)
                theodoi = true;
            else theodoi = false;

            if (txtMaHang.Text == "")
            {
                HangHoa.ThemMoi( new HangHoa (txtTenHang.Text,Int16.Parse(cmbLoaiHang.SelectedValue.ToString()),Int16.Parse(txtSoLuongTon.Text),
                    double.Parse(txtGiavon.Text), double.Parse(txtGiaBan.Text), txtDonvitinh.Text, txtMoTa.Text, theodoi));
                      
            }
            else
            {
                HangHoa.CapNhat(new HangHoa (txtMaHang.Text, txtTenHang.Text, Int16.Parse(cmbLoaiHang.SelectedValue.ToString()), Int16.Parse(txtSoLuongTon.Text),
                    double.Parse(txtGiavon.Text), double.Parse(txtGiaBan.Text), txtDonvitinh.Text, txtMoTa.Text, theodoi));
            }
                LoadDanhSachHangHoa();

           
        }

        private bool KiemTra() 
        {
            if (txtTenHang.Text == "" || cmbLoaiHang.SelectedValue == null ||
                txtDonvitinh.Text == "" || txtGiaBan.Text == ""

                || txtGiavon.Text == "" || txtMoTa.Text == "")
            {
                MessageBox.Show("Nhập đầy đủ thông tin!");
                txtTenHang.Focus();
                return false ;
            }
            return true;
        }

        private void gridviewMDHang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

    }
}
